package minijvm.cmd;

import minijvm.clz.ClassFile;
import minijvm.constant.MethodRefInfo;
import minijvm.engine.ExecutionResult;
import minijvm.engine.MethodArea;
import minijvm.engine.StackFrame;
import minijvm.method.Method;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ChangLiang
 * @date 2020/4/26
 */
public class InvokeSpecialCmd extends TwoOperandCmd {

    public InvokeSpecialCmd(ClassFile clzFile, String opCode) {
        super(clzFile, opCode);
    }

    @Override
    public void execute(StackFrame frame, ExecutionResult result) {
        MethodRefInfo methodRefInfo = (MethodRefInfo) this.getConstantInfo(this.getIndex());
        // 不实现java.lang.Object的init方法
        if (StringUtils.equals("java/lang/Object", methodRefInfo.getClassName())
                && StringUtils.equals("<init>",methodRefInfo.getMethodName())
        ) {
            return;
        }
        // 在方法区中找到下一个方法
        Method method = MethodArea.getInstance().getMethod(methodRefInfo);
        result.setNextAction(ExecutionResult.PAUSE_AND_RUN_NEW_FRAME);
        result.setNextMethod(method);
    }
}
